package com.recruit.service.impl;

import com.google.common.collect.Lists;
import com.recruit.common.util.JsonUtils;
import com.recruit.common.util.RedisUtil;
import com.recruit.dto.chat.MsgInfoDTO;
import com.recruit.mapper.MsgInfoMapper;
import com.recruit.model.MsgInfoDO;
import com.recruit.service.MsgInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author by DHF
 * @Date 2023/03/2023/3/18 23:41
 * @Version 1.0
 */
@Service
public class MsgInfoServiceImpl implements MsgInfoService {
    @Resource
    private MsgInfoMapper msgInfoMapper;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public List<MsgInfoDTO> listMsgInfoById(Integer fromId, Integer toId) {
        String fromMsgKey = fromId + "_" + toId;
        String toMsgKey = toId + "_" + fromId;
        //如果缓存存在信息，就从缓存中取
        if (redisUtil.exists(fromMsgKey) || redisUtil.exists(toMsgKey)) {
            List<MsgInfoDTO> fromMsg = JsonUtils.jsonToList((String) redisUtil.get(fromMsgKey), MsgInfoDTO.class);
            List<MsgInfoDTO> toMsg = JsonUtils.jsonToList((String) redisUtil.get(toMsgKey), MsgInfoDTO.class);
            List<MsgInfoDTO> msgInfoDOS = Stream.of(fromMsg, toMsg)
                    .filter(Objects::nonNull)
                    .flatMap(Collection::stream)
                    .distinct()
                    .collect(Collectors.toList());
            return msgInfoDOS;
        }
        List<MsgInfoDO> msgInfoDOS = msgInfoMapper.selectMsgList(fromId, toId);
        List<MsgInfoDTO> msgInfoDTOS = toMsgInfoDTO(msgInfoDOS);
        //将读到的信息放入缓存中，设置过期时间为半个小时
        redisUtil.set(fromMsgKey, JsonUtils.objectToJson(msgInfoDTOS),  60 * 30);
        return msgInfoDTOS;
    }

    private List<MsgInfoDTO> toMsgInfoDTO(List<MsgInfoDO> msgInfoDOS) {
        if (CollectionUtils.isEmpty(msgInfoDOS)) {
            return Collections.emptyList();
        }
        List<MsgInfoDTO> msgInfoDTOList = Lists.newArrayList();
        msgInfoDOS.forEach(msgInfoDO -> {
            MsgInfoDTO msgInfoDTO = new MsgInfoDTO();
            msgInfoDTO.setId(msgInfoDO.getId());
            msgInfoDTO.setFromUserId(msgInfoDO.getFromUserId());
            msgInfoDTO.setFromUserName(msgInfoDO.getFromUserName());
            msgInfoDTO.setToUserId(msgInfoDO.getToUserId());
            msgInfoDTO.setToUserName(msgInfoDO.getToUserName());
            msgInfoDTO.setContent(msgInfoDO.getContent());
            msgInfoDTO.setUnReadFlag(msgInfoDO.getUnReadFlag());
            msgInfoDTO.setCreateTime(msgInfoDO.getCreateTime());
            msgInfoDTOList.add(msgInfoDTO);
        });
        return msgInfoDTOList;
    }

    @Override
    public void insertMsg(MsgInfoDO msgInfo) {
        initMsg(msgInfo);
        String fromMsgKey = msgInfo.getFromUserId() + "_" + msgInfo.getToUserId();
        String toMsgKey = msgInfo.getToUserId() + "_" + msgInfo.getFromUserId();
        //做新增操作时，将缓存中的信息删除
        redisUtil.delete(fromMsgKey, toMsgKey);
        int insert = msgInfoMapper.insert(msgInfo);
        if (insert == 0) {
            throw new RuntimeException("插入失败");
        }
    }

    private void initMsg(MsgInfoDO msgInfo) {
        //给消息添加时间和未读标识
        msgInfo.setCreateTime(new Date());
        msgInfo.setUnReadFlag(0);
    }
}
